package cn.org.timespace.easyframe.util.itext;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

public class PrintUtil {
	
	/**
	 * @print 单据打印
	 * @param templePdfPath 模版路径
	 * @param newPdfPath  新单据路径
	 * @param map  key == 模版text中的name  value就是要在模版中显示的值
	 * @author wengqd
	 * @date  2009-09-15
	 */
	
	public static boolean print(String templePdfPath,String newPdfPath,Map map,HttpServletResponse response){
		
		boolean flag = true;
		
	   	try {
			FileOutputStream fos = new FileOutputStream(newPdfPath);//需要生成PDF
			
			ByteArrayOutputStream baos = new ByteArrayOutputStream();//用于存储每页生成PDF流
			/** 向PDF模板中插入数据 */
			
			PdfReader reader = new PdfReader(templePdfPath);
			
			PdfStamper stamp = new PdfStamper(reader, baos);
			
			AcroFields acroFields = stamp.getAcroFields();
			//显示中文 必须 在下面重新设置属性  setFieldProperty
			BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", true); 
			
			Iterator iter = map.entrySet().iterator(); 
			while (iter.hasNext()) { 
			    Map.Entry entry = (Map.Entry) iter.next(); 
			    Object key = entry.getKey(); 
			    Object val = entry.getValue(); 
			    acroFields.setFieldProperty(key.toString(),"textfont",bfChinese,null);
			    acroFields.setField(key.toString(),val.toString());//（模版中text的名称，显示的值）  
			}
			
			//acroFields.setField("DepartmnetNmae", "翁1");//插入的数据都为字符类型
			//acroFields.setField("qq", "363596176");				
		
			stamp.setFormFlattening(true); // 显示  设值
			stamp.close();
			
			Document doc = new Document();
			PdfCopy pdfCopy = new PdfCopy(doc, fos);
			doc.open();
			
			PdfImportedPage impPage = pdfCopy.getImportedPage(new PdfReader(baos.toByteArray()), 1);
			pdfCopy.addPage(impPage);

			doc.close();//当文件拷贝 关闭doc
			
			
//			response.setContentType("application/pdf");       
//	        /* 如果想出来让IE提示你是打开还是保存的对话框，加上下面这句就可以了 */      
//	        response.setHeader("Content-disposition","attachment; filename="+"report.pdf" );   
//	        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");      
//	        response.setHeader("Pragma", "public");      
//	        response.setDateHeader("Expires", (System.currentTimeMillis() + 1000));   
//	        response.setContentLength(baos.size());       
//	        ServletOutputStream out = response.getOutputStream();       
//	        baos.writeTo(out);       
//            out.flush();       
//            out.close();       
//            baos.close();    
			
			response.setContentType("application/pdf");

			response.setContentLength(baos.size());

			ServletOutputStream out = response.getOutputStream();

			baos.writeTo(out);

			out.flush();


			
		} catch (IOException e) {
			flag=false;
			e.printStackTrace();
		} catch (DocumentException e) {
			flag=false;
			e.printStackTrace();
		}
		return flag;
	}
	
}
